{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def probability(n, k, p, all_probs=False):\n",
    "    F = np.zeros(n + 1)\n",
    "    U = np.zeros(n + 1)\n",
    "    R = p ** np.arange(k + 1)\n",
    "\n",
    "    for i in range(k, n + 1):\n",
    "        U[i] = R[k] - R[1:k] @ U[i-k+1:i][::-1]\n",
    "        F[i] = U[i] - F[1:i] @ U[1:i][::-1]\n",
    "\n",
    "    S = F.cumsum()\n",
    "\n",
    "    return S if all_probs else S[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## run"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def print_chance(n):\n",
    "    print(n, 'tosses; probability to see at least ...')\n",
    "    for k in range(1, n + 1):\n",
    "        p = probability(n, k, .5)\n",
    "        print('%dx HEADS in row = %.4f' % (k, p))\n",
    "        if p < 1e-4:\n",
    "            break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 tosses; probability to see at least ...\n",
      "1x HEADS in row = 0.9375\n",
      "2x HEADS in row = 0.5000\n",
      "3x HEADS in row = 0.1875\n",
      "4x HEADS in row = 0.0625\n"
     ]
    }
   ],
   "source": [
    "print_chance(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 tosses; probability to see at least ...\n",
      "1x HEADS in row = 0.9990\n",
      "2x HEADS in row = 0.8594\n",
      "3x HEADS in row = 0.5078\n",
      "4x HEADS in row = 0.2451\n",
      "5x HEADS in row = 0.1094\n",
      "6x HEADS in row = 0.0469\n",
      "7x HEADS in row = 0.0195\n",
      "8x HEADS in row = 0.0078\n",
      "9x HEADS in row = 0.0029\n",
      "10x HEADS in row = 0.0010\n"
     ]
    }
   ],
   "source": [
    "print_chance(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50 tosses; probability to see at least ...\n",
      "1x HEADS in row = 1.0000\n",
      "2x HEADS in row = 1.0000\n",
      "3x HEADS in row = 0.9827\n",
      "4x HEADS in row = 0.8274\n",
      "5x HEADS in row = 0.5519\n",
      "6x HEADS in row = 0.3146\n",
      "7x HEADS in row = 0.1653\n",
      "8x HEADS in row = 0.0836\n",
      "9x HEADS in row = 0.0415\n",
      "10x HEADS in row = 0.0204\n",
      "11x HEADS in row = 0.0100\n",
      "12x HEADS in row = 0.0049\n",
      "13x HEADS in row = 0.0024\n",
      "14x HEADS in row = 0.0012\n",
      "15x HEADS in row = 0.0006\n",
      "16x HEADS in row = 0.0003\n",
      "17x HEADS in row = 0.0001\n",
      "18x HEADS in row = 0.0001\n"
     ]
    }
   ],
   "source": [
    "print_chance(50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000 tosses; probability to see at least ...\n",
      "1x HEADS in row = 1.0000\n",
      "2x HEADS in row = 1.0000\n",
      "3x HEADS in row = 1.0000\n",
      "4x HEADS in row = 1.0000\n",
      "5x HEADS in row = 1.0000\n",
      "6x HEADS in row = 0.9997\n",
      "7x HEADS in row = 0.9818\n",
      "8x HEADS in row = 0.8611\n",
      "9x HEADS in row = 0.6242\n",
      "10x HEADS in row = 0.3854\n",
      "11x HEADS in row = 0.2154\n",
      "12x HEADS in row = 0.1140\n",
      "13x HEADS in row = 0.0586\n",
      "14x HEADS in row = 0.0297\n",
      "15x HEADS in row = 0.0150\n",
      "16x HEADS in row = 0.0075\n",
      "17x HEADS in row = 0.0038\n",
      "18x HEADS in row = 0.0019\n",
      "19x HEADS in row = 0.0009\n",
      "20x HEADS in row = 0.0005\n",
      "21x HEADS in row = 0.0002\n",
      "22x HEADS in row = 0.0001\n",
      "23x HEADS in row = 0.0001\n"
     ]
    }
   ],
   "source": [
    "print_chance(1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
